﻿using System;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx.Common;


namespace DataMiner
{
    public class Util
    {

        public Util()
        {
        }

        public static void debug(string format, params object[] args)
        {
            if (DataMinerSettings.Instance.debug)
                Logging.Write("[DataMiner]:" + format, args);
        }

        public static void slog(string format, params object[] args)
        {
            Logging.Write("[DataMiner]:" + format, args);
        }

        public static string HttpPost(string url, object param)
        {
            {

                try
                {
                    HttpWebRequest req = WebRequest.Create(new Uri(url))
                                         as HttpWebRequest;
                    req.Method = "POST";
                    req.ContentType = "application/json; charset=utf-8";

                    
                    // Encode the parameters as form data:
                    byte[] formData =
                        UTF8Encoding.UTF8.GetBytes(JSON.JsonEncode(param));
                    req.ContentLength = formData.Length;
                    debug("Sending : JSON:{0}", JSON.JsonEncode(param));
                    
                    
                    // Send the request:
                    using (Stream post = req.GetRequestStream())
                    {
                        post.Write(formData, 0, formData.Length);
                    }

                    debug("Sending : Address:{0} Method:{1}", req.Address, req.Method);
                    
                    // Pick up the response:
                    string result = null;
                    using (HttpWebResponse resp = req.GetResponse()
                                                  as HttpWebResponse)
                    {
                        debug("Response ContentType : {0}", resp.ContentType);
                        StreamReader reader =
                            new StreamReader(resp.GetResponseStream());
                        result = reader.ReadToEnd();
                    }
                    return result;
                }
                catch (WebException e)
                {
                    Stream responseStream = e.Response.GetResponseStream();
                    StreamReader responseReader = new StreamReader(responseStream);

                    string responseString = responseReader.ReadToEnd();
                    slog("Erreur Web  retour: {0} {1} {2}", e.Status, e.Message, responseString);
                    return "";
                }

                catch (Exception e)
                {
                    slog("Exception: {0} {1}", e.Message, e.InnerException);
                    return "";
                }
            }

        
        }

        public static string HttpPost(string url, string[] paramName, string[] paramVal)
        {
            try
            {
                HttpWebRequest req = WebRequest.Create(new Uri(url))
                                     as HttpWebRequest;
                req.Method = "POST";
                req.ContentType = "application/json; charset=utf-8";
                StringBuilder paramz = new StringBuilder();
                for (int i = 0; i < paramName.Length; i++)
                {
                    paramz.Append(paramName[i]);
                    paramz.Append("=");
                    paramz.Append(System.Net.WebUtility.HtmlDecode(paramVal[i]));
                    paramz.Append("&");
                }

                // Encode the parameters as form data:
                byte[] formData =
                    UTF8Encoding.UTF8.GetBytes(paramz.ToString());
                req.ContentLength = formData.Length;

                // Send the request:
                using (Stream post = req.GetRequestStream())
                {
                    post.Write(formData, 0, formData.Length);
                }

                // Pick up the response:
                string result = null;
                using (HttpWebResponse resp = req.GetResponse()
                                              as HttpWebResponse)
                {
                    StreamReader reader =
                        new StreamReader(resp.GetResponseStream());
                    result = reader.ReadToEnd();
                }
                return result;
            }
            catch (WebException e)
            {
                Stream responseStream = e.Response.GetResponseStream();
                StreamReader responseReader = new StreamReader(responseStream);

                string responseString = responseReader.ReadToEnd();
                if (DataMinerSettings.Instance.debug) 
                    Logging.Write(string.Format("[DataMiner]: Echec: {0}", responseString));
                return "";
            }

            catch (Exception e)
            {
                Logging.Write(string.Format("[DataMiner]: Echec: {0}", e.Message));
                return "";
            }
        }

    }
}
